home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
rjs.lha
/
RJS
/
KernelVar
/
src
/
KernelVar.C
next >
Wrap
C/C++ Source or Header
|
1991-06-14
|
4KB
|
184 lines
#include <stdio.h>
#include <iostream.h>
#include <machine/param.h>
#ifdef mips
#include <sys/fixpoint.h>
#endif
#include<libc.h>
#include<osfcn.h>
#include "KernelVar.h"
#include <sys/cpudata.h>
const char *KMEM="/dev/kmem";
const char *VMUNIX="/vmunix";
#define N_AVENRUN 3
enum KernVars { // MUST BE KEPT CONSISTANT WITH nlist struct
k_avenrun,
k_ifnet,
k_physmem,
k_freemem,
k_boottime,
k_total,
k_endofvars
};
static struct nlist nl[] = {
{ "_avenrun" }, /* load average */
{ "_ifnet" }, /* network interface */
{ "_physmem" }, /* physical memory */
{ "_freemem" }, /* free memory */
{ "_boottime"}, /* boot time */
{ "_total" }, /* vm stats */
{ 0 }
};
int RJS_KernelVar::init=0;
int RJS_KernelVar::kmem=0;
RJS_KernelVar::RJS_KernelVar()
{
if (!init++) {
if ((kmem = open(KMEM, O_RDONLY)) < 0) {
printf("KernelVar: open failed for %s\n",KMEM);
exit(1);
}
if (nlist(VMUNIX,nl) <0) {
printf("KernelVar: no namelist for %s\n",VMUNIX);
exit(1);
}
}
}
RJS_KernelVar::~RJS_KernelVar()
{
if (!--init) close(kmem);
}
int RJS_KernelVar::readk(unsigned long loc, char *buf, int n)
{
if (lseek(kmem, (int) loc, (int) L_SET)==-1) {
cerr << "KernelVar::readk can't lseek in /dev/kmem" << endl;
exit(1);
}
int stat=read(kmem, buf, n);
if (stat==-1) {
cerr << "KernelVar::readk can't read from /dev/kmem" << endl;
exit(1);
}
return stat;
}
int RJS_KernelVar::num_ifnet(int &n)
{
static int num=-1;
if (nl[k_ifnet].n_type==0) return 0;
if (num==-1) {
struct ifnet ifn;
off_t firstif,ip;
ip=(int) nl[k_ifnet].n_value;
readk(ip,(char *)&firstif,sizeof(firstif));
num=0;
for (ip=firstif; ip; ip = (off_t) ifn.if_next) {
readk(ip,(char *)&ifn,sizeof(ifn));
num++;
}
}
n=num;
return 1;
}
int RJS_KernelVar::get_ifnet(int i, kern_ifnet &kifnet)
{
int num;
if (!num_ifnet(num) || i<0 || i>=num) return 0;
struct ifnet ifn;
off_t firstif,ip;
ip=(int) nl[k_ifnet].n_value;
readk(ip,(char *)&firstif,sizeof(firstif));
int n=0;
for (ip=firstif; ip; ip = (off_t) ifn.if_next) {
readk(ip,(char *)&ifn,sizeof(ifn));
if (n==i) {
readk((unsigned long)ifn.if_name,kifnet.if_name,IFNAMSIZ);
kifnet.if_unit = ifn.if_unit;
kifnet.if_flags = ifn.if_flags;
kifnet.if_ipackets = ifn.if_ipackets;
kifnet.if_ierrors = ifn.if_ierrors;
kifnet.if_opackets = ifn.if_opackets;
kifnet.if_oerrors = ifn.if_oerrors;
kifnet.if_collisions = ifn.if_collisions;
return 1;
}
n++;
}
return 0;
}
int RJS_KernelVar::loadav(kern_loadav &kla)
{
if (nl[k_avenrun].n_type==0) return 0;
#ifdef vax
double kern_avenrun[3];
#elif mips
fix kern_avenrun[3];
#endif
readk( nl[k_avenrun].n_value, (char *) kern_avenrun,sizeof(kern_avenrun));
for (int i=0; i<3; i++) {
#ifdef vax
kla.la[i] = kern_avenrun[i];
#elif mips
kla.la[i] = FIX_TO_DBL(kern_avenrun[i]);
#endif
}
return 1;
}
int RJS_KernelVar::pages_to_k(int n)
{
if (NBPG < 1024) return n / (1024/NBPG);
else return n*(NBPG/1024);
}
int RJS_KernelVar::physmem(int &mem)
{
if (nl[k_physmem].n_type==0) return 0;
readk( nl[k_physmem].n_value, (char *) &mem,sizeof(mem));
return 1;
}
int RJS_KernelVar::freemem(int &mem)
{
if (nl[k_freemem].n_type==0) return 0;
readk( nl[k_freemem].n_value, (char *) &mem,sizeof(mem));
return 1;
}
int RJS_KernelVar::boottime(struct timeval &boot)
{
if (nl[k_boottime].n_type==0) return 0;
readk( nl[k_boottime].n_value, (char *) &boot,sizeof(boot));
return 1;
}
int RJS_KernelVar::vmstats(struct vmtotal &vt)
{
if (nl[k_total].n_type==0) return 0;
readk( nl[k_total].n_value, (char *) &vt,sizeof(vt));
return 1;
}